package com.zuo.seckilldemo.controller;

import com.zuo.seckilldemo.entity.User;
import com.zuo.seckilldemo.result.Result;
import com.zuo.seckilldemo.service.GoodsService;
import com.zuo.seckilldemo.utils.DetailVo;
import com.zuo.seckilldemo.utils.UserThreadLocal;
import com.zuo.seckilldemo.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zuogaocan
 * @since 2023-01-31
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @RequestMapping("/to_list")
    @ResponseBody
    public String toList(Model model,HttpServletRequest request,HttpServletResponse response){
        User user = UserThreadLocal.get();
        ValueOperations ops = redisTemplate.opsForValue();
        String goodsList = (String) ops.get("goodsList");
        if (!StringUtils.isEmpty(goodsList)) return goodsList;
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());

        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        goodsList= thymeleafViewResolver.getTemplateEngine().process("goods_list", webContext);
        if (!StringUtils.isEmpty(goodsList)){
            ops.set("goodsList",goodsList,60, TimeUnit.SECONDS);
        }
        return goodsList;
    }

    @RequestMapping("/detail/{goodsId}")
    @ResponseBody
    public Result toDetail(@PathVariable Long goodsId){    
        User user = UserThreadLocal.get();
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date date = new Date();
        //秒杀状态
        int seckillStatus =1;
        int remainSeconds = 0;
        if (date.after(endDate)) {
            seckillStatus = 2;
            remainSeconds = -1;
        }
        if (date.before(startDate)) {
            seckillStatus=0;
            remainSeconds = ((int) ((startDate.getTime() - date.getTime()) / 1000));
        }
        DetailVo detailVo = new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsVo);
        detailVo.setRemainSeconds(remainSeconds);
        detailVo.setSeckillStatus(seckillStatus);
        return Result.success(200,"成功",detailVo);
    }
    
    
    
//    @RequestMapping("/detail/{goodsId}")
//    @ResponseBody
//    public String toDetail(Model model,@PathVariable Long goodsId,HttpServletRequest request, HttpServletResponse response){
//        ValueOperations ops = redisTemplate.opsForValue();
//        String detail = (String) ops.get("detail:" + goodsId);
//        if (!StringUtils.isEmpty(detail)) return detail;
//        
//        User user = UserThreadLocal.get();
//        model.addAttribute("user",user);
//        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
//        Date startDate = goodsVo.getStartDate();
//        Date endDate = goodsVo.getEndDate();
//        Date date = new Date();
//        //秒杀状态
//        int seckillStatus =1;
//        int remainSeconds = 0;
//        if (date.after(endDate)) {
//            seckillStatus = 2;
//            remainSeconds = -1;
//        }
//        if (date.before(startDate)) {
//            seckillStatus=0;
//            remainSeconds = ((int) ((startDate.getTime() - date.getTime()) / 1000));
//        }
//        
//        model.addAttribute("goods",goodsVo);
//        model.addAttribute("seckillStatus",seckillStatus);
//        model.addAttribute("remainSeconds",remainSeconds);
//
//        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
//        detail= thymeleafViewResolver.getTemplateEngine().process("goods_detail", webContext);
//        if (!StringUtils.isEmpty(detail)){
//            ops.set("detail:"+goodsId,detail,60, TimeUnit.SECONDS);
//        }
//        return detail;
//    }
}
